<template>
  <el-dialog width="500px" title="同意审核" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item>
        <p>请输入手机验证码同意提现审核</p>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" disabled placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="code" label="验证码">
        <div class="flex">
          <el-input v-model="form.code" style="width:150px" placeholder="请输入" autocomplete="off" />
          <div class="inline-block pointer text-center code_btn" type="primary" @click="getPhoneCode()">
            {{ showTime ? timeCount + 's后重新获取' : '获取验证码' }}
          </div>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submit" :loading="isLoading">同意审核</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { withdrawalPass, getExamineMobile } from '@/api/audit'
import { sendMobileCode } from '@/api/login'
export default {
  // props: ['editId'],
  props: {
    type: {
      editId: {
        type: String,
        default: ''
      }
    }
  },
  data() {
    return {
      isLoading: false,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      showTime: false, // 显示60s倒计时
      timeCount: '', // 计时显示
      timer: null // 计时
    }
  },
  methods: {
    getCodes(mobile) {
      sendMobileCode(mobile).then((res) => {
        this.showTime = true
        const TIME_COUNT = 60
        this.timeCount = 60
        this.timer = setInterval(() => {
          if (this.timeCount > 0 && this.timeCount <= TIME_COUNT) {
            this.timeCount--
          } else {
            this.showTime = false
            clearInterval(this.timer)
            this.timer = null
          }
        }, 1000)
      })
    },
    getPhoneCode() {
      if (this.showTime === true) {
        return
      }
      if (!this.form.mobile) {
        this.$message({
          message: '手机号未获取到',
          type: 'warning'
        })
        return
      }
      this.getCodes(this.form.mobile)
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isLoading = true;
          withdrawalPass({ id: this.editId, ...this.form }).then(res => {
            this.dialogVisible = false
            this.isLoading = false
            this.$emit('getList', true)
          }).catch(err => {
            this.dialogVisible = false
            this.isLoading = false
            this.$emit('getList', true)
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    },
    getMobile() {
      getExamineMobile().then(res => {
        this.form.mobile = res.mobile || ''
      })
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.getMobile()
      } else {
        this.$refs['ruleForm'].resetFields()
        this.form = {
          mobile: '',
          code: ''
        }
        this.showTime = false
        this.timeCount = ''
        this.timer = null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.code_btn {
  width: 130px;
  margin-left: 20px;
  color: #ffffff;
  border-radius: 3px;
  background-color: $mainBlue;
}
</style>
